import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpResponse, HttpHeaders } from '@angular/common/http';
import { Observable, of, Subject } from 'rxjs';
import { catchError, filter, map } from 'rxjs/operators';
import { ConfigService } from 'app/config/config.service';
import { SELECT_OPTIONS } from 'app/app.constants';

export const httpOptions: Object = {
    headers: new HttpHeaders({
        'Content-Type': 'application/json'
    })
};
export const httpOptions2: Object = {
    headers: new HttpHeaders({
        'Content-Type': 'application/json'
    }),
    responseType: 'blob'
};

@Injectable({
    providedIn: 'root'
})
export class PublicService {
    private apiUrl_vms_online = this.config.apiUrl.vms;
    private apiUrl_enum = this.config.apiUrl.enum;
    public optionsList: any[] = [];

    constructor(private http: HttpClient, private config: ConfigService) {}

    getVmsCurrentStatusById(deviceId): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/getVmsCurrentStatusById?eqtId=${deviceId}`;
        return this.http.get<any>(url);
    }

    setVmsIllumBrightness(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/setVmsIllumBrightness`;
        return this.http.post<any>(url, params, httpOptions);
    }

    sendVmsMessage(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/sendVmsMessage`;
        return this.http.post<any>(url, params, httpOptions);
    }

    getVmsCurrentMessageById(deviceId): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/getVmsCurrentMessageById`;
        return this.http.get<any>(url, { params: { deviceId } });
    }

    getAllVmsLibraryMessage(): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/getAllVmsLibraryMessage`;
        return this.http.get<any>(url);
    }

    // 上传base64码图片
    uploadImgByBase64(params: { strBase64: string; graphicName: string }): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/uploadBase64`;
        return this.http.post<any>(url, params, httpOptions);
    }

    export(params): Observable<any> {
        return this.http.post<any>(params.url, params.params, httpOptions2);
    }

    // 下拉列表同步到redis，login成功后调用一次
    syncEnumInfoToRedis(): Observable<any> {
        const url = `${this.apiUrl_enum}/vnqnenummgt/user/syncEnumInfoToRedis`;
        return this.http.get<any>(url);
    }

    getSelectOptionsListFromBackend() {
        const url = `${this.apiUrl_enum}/vnqnenummgt/user/getAllEnumInfo`;
        return this.http.get<any>(url);
    }

    formatList(data, type?: SELECT_OPTIONS) {
        const newList = data.filter(item => item.category === type);
        // if (isAddAll) {
        //     newList.unshift({ name: 'All', value: null });
        // }
        const newObject = {};
        newList.sort((a, b) => a.value - b.value).map(item => {
            newObject[item.value] = item.name;
        });
        return { data: newList, object: newObject };
    }

    // 获取下拉框列表
    getSelectOptionsList(type?: SELECT_OPTIONS, isAddAll?: Boolean): { data: Array<any>; object: Object } {
        return this.formatList(this.optionsList, type);
    }

    getButtonAccess(currentMenu: string, buttonObj?: any) {
        const allAccessList = JSON.parse(localStorage.getItem('menu'));
        const filterList = allAccessList.filter(item => item.parentId === +currentMenu);
        filterList.forEach(item => {
            for (const keys in buttonObj) {
                if (item.rightName === keys) {
                    buttonObj[keys] = true;
                }
            }
        });
        return buttonObj;
    }
}
